package com.ruoyi.quartz.task;

import com.ruoyi.adapayRefund.domain.AdapayRefund;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.coupons.service.ICouponsMemberService;
import com.ruoyi.coupons.service.ICouponsService;
import com.ruoyi.order.domain.StoreProductOrder;
import com.ruoyi.order.service.IStoreProductOrderHandleService;
import com.ruoyi.order.service.IStoreProductOrderService;
import com.ruoyi.product.service.IStoreProductBackService;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.service.IMallOrderService;
import com.ruoyi.withdraw.service.IWithdrawApplyHandleService;
import com.ruoyi.withdraw.service.IWithdrawApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chengliang
 * @date 2022/2/17
 **/
@Component("refundTask")
public class RefundTask {
    private static final Logger log = LoggerFactory.getLogger(RefundTask.class);

    @Autowired
    private IStoreProductOrderService orderService;
    @Autowired
    private IMallOrderService mallOrderService;
    public void refundTask(Integer minute)
    {
        List<Long> ids = orderService.selectNeedRefund(minute);
        if(ids.size()==0)
            return;
//        ArrayList<StoreProductOrder> badOrder = new ArrayList<>();
        ids.forEach(e->{
            StoreProductOrder order = orderService.selectStoreProductOrderById(e);
            if(DicConstants.ORDER_STATE_Wait_Use.getCode().equals(order.getOrderState())){
                //未使用的才可以取消支付
                AdapayRefund adapayRefund = orderService.refundPay(order.getId());
                if(null!=adapayRefund){

                    orderService.refund(order,adapayRefund);
                }else{
                    //badOrder.add(order);
                }
            }
        });
    }
   /* public void refundMallOrderTask(Integer minute)
    {
        List<Long> ids = orderService.selectNeedRefund(minute);
        if(ids.size()==0)
            return;
        ids.forEach(e->{
            MallOrder order = mallOrderService.selectMallOrderById(e);
            if(DicConstants.ORDER_STATE_Wait_Use.getCode().equals(order.getOrderState())){
                AdapayRefund adapayRefund = mallOrderService.refundPay(order.getId());
                if(null!=adapayRefund){

                    mallOrderService.refund(order,adapayRefund);
                }
            }
        });
    }*/

    public void orderOverdue(){
        List<StoreProductOrder> orders = orderService.selectOverdue();
        orders.forEach(e->{

            e.setRefundPrice(e.getPayPrice());
            e.setRefundReason("订单超时,自动退款");
            e.setRefundTime(new Date());
            //暂时不需要审核
            e.setRefundStatus(DicConstants.REFUND_STATUS_Pass.getCode());
            e.setAftersaleState(DicConstants.SYS_Y.getCode());

            orderService.updateStoreProductOrder(e);
        });
    }
}
